<template>
  <!-- 添加或修改请购单对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-form-item label="请购人" prop="userId">
              <el-select v-model="form.userId" placeholder="请选择请购人" filterable @change="selectUser" clearable >
                <el-option v-for="dict in dict.type.sys_user" ref="myselectUser"
                           :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { addAction, getById, updateAction } from '@/api/manage';
  import moment from 'moment';

  export default {
    name: "LvDoForm",
    dicts: ['sys_user'],
    data() {
      return {
        // 遮罩层
        loading: true,
        activeName: "first",
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          userId: [
            {required: true, message: "不能为空", trigger: "blur"},
          ],
        },
        url: {
          getById:'/lvDo/lvDo/',
          add:'/lvDo/lvDo/add',
          edit:'/lvDo/lvDo/edit',
        },
      };
    },
    created() {

    },
    methods: {
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          code: null,
          name: null,
          mainId: null,
          nos: null,
          ddDate: null,
          subDate: null,
          type: null,
          ywType: null,
          tax: null,
          customerId: null,
          customerName: null,
          isBuy: null,
          money: null,
          dingjin: null,
          shengyu: null,
          shuie: null,
          counts: null,
          dunwei: null,
          supplierId: null,
          supplierName: null,
          deptId: null,
          deptName: null,
          userId: null,
          userName: null,
          state: null,
          linkman: null,
          linktel: null,
          idcard: null,
          moneycard: null
        };
        this.resetForm("form");
      },
      selectUser(value) {
        let that = this;
        that.$nextTick(() => {
          let arr = that.$refs.myselectUser;
          for (let i = 0; i < arr.length; i++) {
            if (arr[i].currentValue == value) {
              that.form.userName = arr[i].label;
            }
          }
        })
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids;
        getById(this.url.getById, id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改请购单";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateAction(this.url.edit, this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.$emit('ok');
              });
            } else {
              addAction(this.url.add, this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.$emit('ok');
              });
            }
          }
        });
      },
    }
  };
</script>
